<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>住客入住</title>
    <link rel="stylesheet" href="/static/common/layui/css/layui.css">
    <link rel="stylesheet" href="/static/admin/css/style.css">
    <script src="/static/common/layui/layui.js"></script>
    <script src="/static/common/jquery-3.3.1.min.js"></script>
    <script src="/static/common/vue.min.js"></script>
    <%--    获取系统时间--%>
    <script>
        window.onload = function(){
            function getDate(){
                debugger;
                var today = new Date();
                var date;
                date = (today.getFullYear()) +"-" + (today.getMonth() + 1 ) + "-" + today.getDate() + "-" + today.toLocaleTimeString();
                return date;
            }
            window.setInterval(function(){
                document.getElementById("getTime").value=getDate();
            }, 1000);
        }

        function loadData(){
            $.post("/BooksServlet.do",{"action":"loadData"},function(data){
                var vips=data.vips;
                var homes=data.homes;

                for (let i = 0; i < vips.length; i++) {
                    var id= vips[i].id;
                    var name= vips[i].name;
                    $("[name='vip_id']").append("<option value='"+id+"'>"+name+"</option>");
                }
                for (let i = 0; i < homes.length; i++) {
                    var id= homes[i].id;
                    var num= homes[i].num;
                    var h_type= homes[i].h_type;
                    $("[name='home_id']").append("<option value='"+id+"'>"+num+" "+h_type+"</option>");
                }
            },"json");
        }

        function addBook(){
            var vip_id=$("[name='vip_id']").val();
            var home_id=$("[name='home_id']").val();
            var enterTime=$("[name='enterTime']").val();
            var exitTime=$("[name='exitTime']").val();

            if(vip_id=="" || home_id=="" || enterTime=="" || exitTime=="" ){
                layer.msg("请填写完整信息");
                return;
            }
            if(!checkExitTime()){
                return;
            }

            $.post("/BooksServlet.do",{
                "action":"addBook",
                "vip_id":vip_id,
                "home_id":home_id,
                "enterTime":enterTime,
                "exitTime":exitTime
            },function(data){
                if(data == true){
                    alert("入住成功");
                    window.location.href="/views/guests_list.jsp";
                }else{
                    alert("入住失败，请稍后再试");
                }
            },"json");
        }

        function checkExitTime(){
            let startTime =new Date($("input[name='enterTime']").val());
            let endTime = new Date($("input[name='exitTime']").val());
            if(endTime<startTime){
                $("input[name='exitTime']").next("span").text("退房时间必须大于入住时间");
                return false;
            }else{
                $("input[name='exitTime']").next("span").text("");
                return true;
            }

        }

    </script>

</head>
<body onload="loadData();">
<div id="app">
    <!--顶栏-->
    <%--    直接用top.jsp--%>
    <jsp:include page="top.jsp" flush="true"/>

    <div class="main" id="app">
        <!--左栏-->
        <div class="left">
            <ul class="cl" >
                <!--顶级分类-->
                <li v-for="vo,index in menu" :class="{hidden:vo.hidden}">
                    <a href="javascript:;"  :class="{active:vo.active}"  @click="onActive(index)">
                        <i class="layui-icon" v-html="vo.icon"></i>
                        <span v-text="vo.name"></span>
                        <i class="layui-icon arrow" v-show="vo.url.length==0">&#xe61a;</i> <i v-show="vo.active" class="layui-icon active">&#xe623;</i>
                    </a>
                    <!--子级分类-->
                    <div v-for="vo2,index2 in vo.list">
                        <a href="javascript:;" :class="{active:vo2.active}" @click="onActive(index,index2)" v-text="vo2.name"></a>
                        <i v-show="vo2.active" class="layui-icon active">&#xe623;</i>
                    </div>
                </li>
            </ul>
        </div>
        <!--右侧-->
        <div class="right">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>添加住客</legend>
            </fieldset>

                <div class="layui-form-item">
                    <label class="layui-form-label">入住会员</label>
                    <div class="layui-input-block">
                        <select name="vip_id" class="layui-input" autocomplete="off" required >

                        </select>
                    </div>
                </div>

                <div class="layui-form-item" >
                    <label class="layui-form-label">入住时间</label>
                    <div class="layui-input-block" >
                        <input type="date" name="enterTime" required id="getTime"  autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">退房时间</label>
                    <div class="layui-input-block">
                        <input type="date" name="exitTime" required  id="" autocomplete="off" onblur="checkExitTime()" class="layui-input">
                        <span style="color: red"></span>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">房间号</label>
                    <div class="layui-input-block">
                        <select name="home_id" class="layui-input" autocomplete="off" required >

                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" type="button" onclick="addBook();">办理入住</button>
                        <button class="layui-btn layui-btn-danger" type="button" onclick="javascript:history.go(-1);">退出添加</button>
                    </div>
                </div>

        </div>
    </div>
</div>
<script src="/static/admin/js/config.js"></script>
<script src="/static/admin/js/script.js"></script>
</body>
</html>
